/*
 * Name: Minh-Tuan ta
 * Class: CreateComment.java
 */

package actions;

import java.io.IOException;

import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.CommentDB;
import data.ContentTypeDB;

import beans.Comment;
import beans.User;

public class CreateComment extends Action {

	@Override
	protected void setURLMapping() {
		// TODO Auto-generated method stub
		urlMapping = "/CreateComment";
	}

	@Override
	protected String doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("User");
		String url = getLastURL(request, session);
		
		Comment comment = new Comment();
		
		comment.setContent(request.getParameter("commentContent"));
		comment.setContentTitle("");
		comment.setContentTypeID(ContentTypeDB.getContentTypeID("Comment"));
		comment.setDateCreated(new Timestamp((new java.util.Date()).getTime()));
		comment.setExtra(Integer.parseInt(request.getParameter("postID")));
		comment.setUserID(user.getUserID());
		
		if (CommentDB.addComment(comment) == 1)
		{
			session.setAttribute("ReturnStatus", "Comment Added");
		}
		else
		{
			session.setAttribute("ReturnStatus", "Comment Not Added");
		}
		
		return url;
	}

	@Override
	protected void setupPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}
}
